package com.ruoyi.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.course.domain.SExam;
import com.ruoyi.course.domain.SExamOption;
import com.ruoyi.course.domain.SExamQuestion;
import com.ruoyi.course.mapper.SExamOptionMapper;
import com.ruoyi.course.mapper.SExamQuestionMapper;
import com.ruoyi.course.vo.SExamQuestionVo;
import com.ruoyi.course.vo.SExamVo;
import com.ruoyi.course.mapper.SExamMapper;
import com.ruoyi.course.service.ISExamService;
import com.ruoyi.course.vo.SaveAnswerVO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.teacher.domain.TExam;
import com.ruoyi.teacher.domain.TOption;
import com.ruoyi.teacher.domain.TPaperQuestion;
import com.ruoyi.teacher.mapper.TExamMapper;
import com.ruoyi.teacher.mapper.TOptionMapper;
import com.ruoyi.teacher.mapper.TPaperQuestionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生考试Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class SExamServiceImpl extends ServiceImpl<SExamMapper, SExam> implements ISExamService {


    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    TExamMapper examMapper;
    @Autowired
    SExamQuestionMapper sExamQuestionMapper;
    @Autowired
    SExamOptionMapper sExamOptionMapper;
    @Autowired
    TPaperQuestionMapper paperQuestionMapper;
    @Autowired
    TOptionMapper tOptionMapper;
    @Override
    public TableDataInfo<SExamVo> queryList(SExamVo entity) {
        Page<SExamVo> sExamVoPage = this.baseMapper.queryList(PageUtils.buildPage(), entity);
        List<SExamVo> records = sExamVoPage.getRecords();
        for (SExamVo record : records) {
//            获取用户姓名
            String username = null;
            LambdaQueryWrapper<SysUser> userqw = new LambdaQueryWrapper<>();
            userqw.eq(SysUser::getUserId,record.getsId());
            SysUser sysUser = sysUserMapper.selectOne(userqw);
            if (sysUser!=null){
                username = sysUser.getNickName();
            }
//            获取测试名
            String examName = null;
            LambdaQueryWrapper<TExam> examqw = new LambdaQueryWrapper<>();
            examqw.eq(TExam::getId,record.geteId());
            TExam tExam = examMapper.selectOne(examqw);
            if (tExam!=null){
                examName = tExam.getName();
            }
            record.setExamName(examName);
            record.setStudentName(username);
        }
        return PageUtils.buildDataInfo(sExamVoPage);
    }

    @Override
    public List<SExamVo> queryAll(SExamVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public SExamVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public TExam getExam(Integer studentExamId) {
        // 根据studentExamId得到studentExam
        SExam studentExam = baseMapper.selectById(studentExamId);
        // 得到examId
        Long examId = studentExam.geteId();
        // 根据examId获取exam
        return examMapper.selectById(examId);
    }

    @Override
    public List<SExamQuestion> getStudentQuestionList(Integer studentExamId) {
        // 根据studentExamId查询StudentQuestion的List
        QueryWrapper<SExamQuestion> qw = new QueryWrapper<>();
        qw.eq("s_e_id", studentExamId);
        return sExamQuestionMapper.selectList(qw);
    }

    @Override
    public List<List<SExamQuestionVo>> getStudentQuestionVOListList(List<SExamQuestion> studentQuestionList) {
        List<List<SExamQuestionVo>> lists=new ArrayList<>();
        for (int i = 0; i < 5; i++) lists.add(new ArrayList<>());
        for (SExamQuestion cur:studentQuestionList){
//            创建VO
            SExamQuestionVo sExamQuestionVo = new SExamQuestionVo();
//            复制属性到VO
            BeanUtils.copyProperties(cur,sExamQuestionVo);
            QueryWrapper<SExamOption> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("s_e_q_id",cur.getId());
            List<SExamOption> sExamOptions = sExamOptionMapper.selectList(queryWrapper);
            // 将 SExamOption 转换为 TOption
            List<TOption> tOptions = convertToTOptionList(sExamOptions);
            sExamQuestionVo.setOptions(tOptions);
            int index=sExamQuestionVo.getType().getType();
            lists.get(index).add(sExamQuestionVo);

        }
        return lists;
    }
    // 转换方法：将 SExamOption 列表转换为 TOption 列表
    private List<TOption> convertToTOptionList(List<SExamOption> sExamOptions) {
        List<TOption> tOptions = new ArrayList<>();
        for (SExamOption sExamOption : sExamOptions) {
            TOption tOption = new TOption();

            tOption.setId(sExamOption.getId());
            tOption.setqId(sExamOption.getSEQId());  // sEQId -> qId
            tOption.setCpId(sExamOption.getOId());   // oId -> cpId
            tOption.setIsAnswer(sExamOption.getIsSelected() != null && sExamOption.getIsSelected() == 1 ? 1 : 0); // 假设 1 表示是答案
            tOption.setOrderNum(sExamOption.getOrderNum());
            tOption.setAbc(sExamOption.getAbc());
            tOption.setRemark(sExamOption.getRemark());

            tOptions.add(tOption);
        }
        return tOptions;
    }


    @Override
    public TPaperQuestion getPaperQuestion(Integer paperId, Integer questionId) {
        QueryWrapper<TPaperQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("p_id",paperId)
                .eq("q_id",questionId);
        return paperQuestionMapper.selectOne(queryWrapper);
    }

    @Override
    public List<TOption> getOptions(Long id) {
        QueryWrapper<TOption> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("q_id", id);
        return tOptionMapper.selectList(queryWrapper1);
    }

    @Override
    public boolean updateAnswer(SaveAnswerVO saveAnswerDTO) {
        return false;
    }
}
